###R Script for Analyses in Manuscript 
###"Thinking Generically and Specifically in IR Survey Experiments"
###by Clara H. Suong, Scott Desposato, and Erik Gartzke
###R version 4.2.1 (2022-06-23)
###Platform: x86_64-apple-darwin17.0    

rm(list=ls())
library(foreign)

#setwd("./rap_submission/")

### Start writing to an output file
sink('analysis_output.txt')

###Import data
brazil<-read.csv(file = "brazil.csv", header=TRUE, na.strings=c("","NA")) 
china<-read.csv(file = "china.csv", header=TRUE, na.strings=c("","NA")) 
japan<-read.csv(file = "japan.csv", header=TRUE, na.strings=c("","NA")) 
sweden<-read.csv(file = "sweden.csv", header=TRUE, na.strings=c("","NA")) 
ukraine<-read.csv(file = "ukraine.csv", header=TRUE, na.strings=c("","NA")) #No UN treatment

###Figure 1
cat("===================================================\n")
cat("Figure 1: Percentage of Compliers and Non-Compliers\n")
cat("===================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
100*prop.table(table(brazil$specific))

cat("=====\n")
cat("China\n")
cat("=====\n")
100*prop.table(table(china$specific))

cat("=====\n")
cat("Japan\n")
cat("=====\n")
100*prop.table(table(japan$specific))

cat("======\n")
cat("Sweden\n")
cat("======\n")
100*prop.table(table(sweden$specific))

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
100*prop.table(table(ukraine$specific))


###Figure 2
cat("==================================================\n")
cat("Figure 2: Schema Inconsistency Among Non-Compliers\n")
cat("==================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
100*prop.table(table(brazil$sch_i))

cat("=====\n")
cat("China\n")
cat("=====\n")
100*prop.table(table(china$sch_i))

cat("=====\n")
cat("Japan\n")
cat("=====\n")
100*prop.table(table(japan$sch_i))

cat("======\n")
cat("Sweden\n")
cat("======\n")
100*prop.table(table(sweden$sch_i))

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
100*prop.table(table(ukraine$sch_i))


###Figure 3
cat("===================================================================\n")
cat("Figure 3: Target Regime Treatment Inconsistency Among Non-Compliers\n")
cat("===================================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
100*prop.table(table(brazil$demo_i))

cat("=====\n")
cat("China\n")
cat("=====\n")
100*prop.table(table(china$demo_i))

cat("=====\n")
cat("Japan\n")
cat("=====\n")
100*prop.table(table(japan$demo_i))

cat("======\n")
cat("Sweden\n")
cat("======\n")
100*prop.table(table(sweden$demo_i))

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
100*prop.table(table(ukraine$demo_i))


###Main analysis 
##Figure 4, Table 11
cat("==================================================================\n")
cat("Figure 4, Table 11: Estimated Changes in ATE Due to Non-Compliance\n")
cat("==================================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
B_bc<-lm(attack.support~(specific)*(un_approval+democracy)
         , data = brazil)
summary(B_bc)

cat("=====\n")
cat("China\n")
cat("=====\n")
C_bc<-lm(attack.support~(specific)*(un_approval+democracy)
         , data = china)
summary(C_bc)

cat("=====\n")
cat("Japan\n")
cat("=====\n")
J_bc<-lm(attack.support~(specific)*(un_approval+democracy)
         , data = japan)
summary(J_bc)

cat("======\n")
cat("Sweden\n")
cat("======\n")
S_bc<-lm(attack.support~(specific)*(un_approval+democracy)
         , data = sweden)
summary(S_bc)

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
U_bc<-lm(attack.support~(specific)*(democracy)
         , data = ukraine)
summary(U_bc)


##Figure 5, Figure 6, Table 16
cat("==================================================================================================\n")
cat("Figure 5: Estimated Changes in ATE Due to Schema Inconsistency Among Non-Compliers\n")
cat("Figure 6: Estimated Changes in ATE Due to Target Regime Treatment Inconsistency Among Non-Compliers\n")
cat("===================================================================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
B_i<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)
         , data = brazil)
summary(B_i)

cat("=====\n")
cat("China\n")
cat("=====\n")
C_i<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)
         , data = china)
summary(C_i)

cat("=====\n")
cat("Japan\n")
cat("=====\n")
J_i<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)
         , data = japan)
summary(J_i)

cat("======\n")
cat("Sweden\n")
cat("======\n")
S_i<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)
         , data = sweden)
summary(S_i)

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
U_i<-lm(attack.support~(sch_i+demo_i)*(democracy)
         , data = ukraine)
summary(U_i)


###Robustness checks 
##Figure 8, Table 12
cat("============================================================================================\n")
cat("Figure 8, Table 12: Estimated Changes in ATE Due to Non-Compliance With Demographic Controls\n")
cat("============================================================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
B_bc_d<-lm(attack.support~(specific)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = brazil)
summary(B_bc_d)

cat("=====\n")
cat("China\n")
cat("=====\n")
C_bc_d<-lm(attack.support~(specific)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = china)
summary(C_bc_d)

cat("=====\n")
cat("Japan\n")
cat("=====\n")
J_bc_d<-lm(attack.support~(specific)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = japan)
summary(J_bc_d)

cat("======\n")
cat("Sweden\n")
cat("======\n")
S_bc_d<-lm(attack.support~(specific)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = sweden)
summary(S_bc_d)

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
U_bc_d<-lm(attack.support~(specific)*(democracy)+
             age + female + education + news 
           , data = ukraine)
summary(U_bc_d)


##Table 17
cat("============================================================================================\n")
cat("Table 17: Non-Compliers’ War Support by Schema and Treatment Inconsistency With Demographics\n")
cat("============================================================================================\n")

cat("======\n")
cat("Brazil\n")
cat("======\n")
B_i_d<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = brazil)
summary(B_i_d)

cat("=====\n")
cat("China\n")
cat("=====\n")
C_i_d<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = china)
summary(C_i_d)

cat("=====\n")
cat("Japan\n")
cat("=====\n")
J_i_d<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = japan)
summary(J_i_d)

cat("======\n")
cat("Sweden\n")
cat("======\n")
S_i_d<-lm(attack.support~(sch_i+demo_i)*(un_approval+democracy)+
             age + female + education + income + news + religious + religiosity
           , data = sweden)
summary(S_i_d)

cat("=======\n")
cat("Ukraine\n")
cat("=======\n")
U_i_d<-lm(attack.support~(sch_i+demo_i)*(democracy)+
             age + female + education + news 
           , data = ukraine)
summary(U_i_d)

# Stop writing to the file
sink()

### Append to the file
sink('analysis_output.txt', append=TRUE)
sink()